package com.example.wanandroid.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wanandroid.R;
import com.example.wanandroid.app.WanAndroidApp;
import com.example.wanandroid.core.dao.HistoryDataDao;
import com.example.wanandroid.core.db.DbHelperImpl;
import com.example.wanandroid.ui.activity.SearchActivity;
import com.example.wanandroid.ui.activity.SearchResultActivity;

import java.util.List;

public class FlowLayoutAdapter extends FlowAdapter<String> {
    DbHelperImpl dbHelper;
    SearchActivity context;
    public FlowLayoutAdapter(Context context, List<String> list) {
        super(context, list);
        this.context= (SearchActivity) context;
    }

    @Override
    public int generateLayout(int position) {
        return R.layout.flow_item;
    }

    @Override
    public void getView(String o, View parent, int position) {
        HistoryDataDao historyDataDao = WanAndroidApp.getInstance().getDaoSession().getHistoryDataDao();
        dbHelper = new DbHelperImpl(historyDataDao);
        final TextView text = parent.findViewById(R.id.flow_text);
        text.setText(o);
        text.setOnClickListener(v->{
            dbHelper.addHistoryData(o);
            context.recreate();
            EditText editText = context.findViewById(R.id.search_edit);
            Intent intent=new Intent(v.getContext(), SearchResultActivity.class);
            intent.putExtra("input",o);
            editText.setText(o);
            v.getContext().startActivity(intent);
        });
    }
}

